<?php
namespace Home\Controller;
use Think\Controller;
class SetController extends CommonController {
    public function index(){
        
        $id=$_SESSION['id'];
        $sql = "select * from user where id='{$id}'";
        $res = M()->query($sql); //查询所匹配的到的id行
        $res=current($res);
        $this->assign('info',$res);//分配$res  在html运用$info

        $this->assign('title','设置'); //分配动态的title，对应的是'设置'
        $this->display();
	
    }



    //上传头像
    public function face()
    {
    	$this->display();
    }

    //upload方法来接美图保存的图片
    public function upload()
    {
    	//var_dump($_FILES);
      /*点击保存时生成的 $_FILES  upload_file数组的下标
      <b>array</b> <i>(size=1)</i>
  'upload_file' <font color='#888a85'>=&gt;</font> 
    <b>array</b> <i>(size=5)</i>
    	 'name' <font color='#888a85'>=&gt;</font> <small>string</small> <font color='#cc0000'>'1.jpg'</font> <i>(length=5)</i>推的图片名
      'type' <font color='#888a85'>=&gt;</font> <small>string</small> <font color='#cc0000'>'image/jpeg'</font> <i>(length=10)</i>推的类型
      'tmp_name' <font color='#888a85'>=&gt;</font> <small>string</small> <font color='#cc0000'>'C:\wamp\tmp\php6478.tmp'</font> <i>(length=23)</i>临时文件所在的位置
      'error' <font color='#888a85'>=&gt;</font> <small>int</small> <font color='#4e9a06'>0</font>错误代码  0正常
      'size' <font color='#888a85'>=&gt;</font> <small>int</small> <font color='#4e9a06'>11349</font>尺寸*/
        $upload = new \Think\Upload();// 实例化上传类
	    $upload->maxSize   =     3145728 ;// 设置附件上传大小
	    $upload->exts      =     array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
	    $upload->rootPath  =     './Uploads/'; // 设置附件上传根目录
	    $upload->savePath  =     ''; // 设置附件上传（子）目录
	    // 上传文件 
	    $info   =   $upload->upload();

	    //var_dump($info);



	    if(!$info) {// 上传错误提示错误信息
	        echo $upload->getError();
	    }else{// 上传成功
	    	$info = current($info);
	    	$path = "/Uploads/".$info['savepath'].$info['savename'];  //上传成功后美图默认要修改的图片路径

	    	$id = $_SESSION['id'];
	    	$face1['face'] = $path;
			//echo $face1;    	
	    	//echo $id;
	    	//echo $path;
	    	//组合后的$path为：Uploads/2017-04-01/58df6db440ba2.jpg 
	    	//$res = M('user')->where(array('id'=>$_SESSION['id']))->save(array('face'=>$path)); 
	    	$res = M('user')->where("id='{$id}'")->save($face1);
	        if($res){
	        	$_SESSION['face']=$path;
	        	echo $path;
	        }else{
	        	echo 0;
	        }

	    }

    }










    //修改信息
    public function add(){
        
        $id=$_SESSION['id'];
        $email=I('post.email'); 
    	$sql = "select * from user where email='{$email}' and id!='{$id}'";
    	$res = M()->query($sql); //查到的除他自己id外 并且 还是跟他一样的邮箱 就是邮箱已存在

    	if ($res) {
    		
    		$arr = array(
    			'error'=>1,
    			'info'=>'邮箱已存在',
    		);
    		echo json_encode($arr);
    		exit; 
    	}

    	$nickname=I('post.nickname');
    	$sql = "select * from user where nickname='{$nickname}' and id!='{$id}'";
    	$res = M()->query($sql);

    	if ($res) {
    		
    		$arr = array(
    			'error'=>1,
    			'info'=>'昵称已存在',
    		);
    		echo json_encode($arr);
    		exit; 
    	}


    	$res = I('post.');

    	$res = M('user')->where("id='{$id}'")->save($res);

    	if ($res){
    		$sql = "select * from user where id='{$id}'";
	    	$res = M()->query($sql);
	    	$res=current($res);

    		$_SESSION['id']=$res['id'];
    		$_SESSION['nickname']=$res['nickname'];	
    		$_SESSION['face']=$res['face'];

    	    	$arr = array(
	    			'error'=>0,
	    			'info'=>'修改成功',
	    		);
	    		echo json_encode($arr);
	    		exit;     	
    	   }else{
	    		$arr = array(
	    			'error'=>1,
	    			'info'=>'修改失败',
	    		);
	    		echo json_encode($arr);
	    		exit;
	    }    	    

    }

    //修改密码
    public function addpaw()
    {
    	$id=$_SESSION['id'];

    	$sql = "select password from user where id='{$id}'";

    	$res = M()->query($sql);
    	$res = current($res);
    	$res = $res['password'];
    	$paw=md5(I('post.nowpass'));
    		
    	//当前密码不等于数据库的密码
    	if ($paw!=$res) {

    		$arr = array(
	    			'error'=>1,
	    			'info'=>'当前密码错误',
	    		);
	    		echo json_encode($arr);
	    		exit;
    	}


    	$password = I('post.password');
    	$repassword = I('post.repassword');

    	if ($password!=$repassword) {
    		$arr = array(
    			'error'=>1,
    			'info'=>'密码不一致',
    		);
    		echo json_encode($arr);
    		exit;
    	}

    	$password=I('post.password');
    	$password1['password']=md5($password);
    	$res = M('user')->where("id='{$id}'")->save($password1);
    	/*$password1=md5($password);
    	$res = M('user')->where("id='{$id}'")->save("$password='{$password1}'");*/
    	if ($res){
    		
    	    	$arr = array(
	    			'error'=>0,
	    			'info'=>'修改成功',
	    		);
	    		echo json_encode($arr);
	    		exit;     	
    	   }else{
	    		$arr = array(
	    			'error'=>1,
	    			'info'=>'修改失败',
	    		);
	    		echo json_encode($arr);
	    		exit;
	    }    	    


    }



    //发送邮件 参数1：收件人邮箱  参数2：邮件名  参数3：邮件内容 调用App/Common/Common/function的linemail方法
    public function sendemail()
    {
        linemail('971141007@qq.com','测试的邮件','要发的内容');
    }



}

?>

